//
//  StopWatchHeader.swift
//  RKTemplate
//
//  Created by zhaoke on 2023/7/24.
//

import UIKit

protocol StopWatchHeaderDelegate {
    func stopWatchPlayAction()
    func stopWatchLapAction()
}

class StopWatchHeader: UIView {
    public var timerLab: UILabel!
    public var lapTimerLab: UILabel!
    var playPauseBtn: UIButton!
    var lapRestBtn: UIButton!
    var delegate: StopWatchHeaderDelegate?
    //
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupChildren()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupChildren() {
        lapTimerLab = {
            let label = UILabel()
            label.font = UIFont.systemFont(ofSize: 16)
            label.textColor = .purple
            label.text = "00:00:00"
            return label
        }()
        timerLab = {
            let label = UILabel()
            label.font = UIFont.systemFont(ofSize: 25)
            label.textColor = .darkText
            label.text = "00:00:00"
            return label
        }()
        playPauseBtn = {
            let playBtn = UIButton(type: .custom)
            playBtn.setTitleColor(.white, for: .normal)
            playBtn.backgroundColor = .orange
            playBtn.layer.cornerRadius = 5
            playBtn.titleLabel?.font = UIFont.systemFont(ofSize: 18)
            playBtn.addTarget(self, action: #selector(playBtnAction(sender:)), for: .touchUpInside)
            playBtn.setTitle("start", for: .normal)
            playBtn.setTitle("stop", for: .selected)
            return playBtn
        }()
        lapRestBtn = {
            let lapBtn = UIButton(type: .custom)
            lapBtn.backgroundColor = .orange
            lapBtn.layer.cornerRadius = 5
            lapBtn.isEnabled = false
            lapBtn.setTitleColor(.white, for: .normal)
            lapBtn.titleLabel?.font = UIFont.systemFont(ofSize: 18)
            lapBtn.addTarget(self, action: #selector(lapBtnAction(sender:)), for: .touchUpInside)
            lapBtn.setTitle("Lap", for: .normal)
            return lapBtn
        }()
        addSubview(lapTimerLab)
        addSubview(timerLab)
        addSubview(playPauseBtn)
        addSubview(lapRestBtn)
        //
        timerLab.snp.makeConstraints { make in
            make.centerX.equalTo(self)
            make.height.equalTo(30)
            make.centerY.equalTo(self).offset(-30)
        }
        lapTimerLab.snp.makeConstraints { make in
            make.right.equalTo(timerLab)
            make.height.equalTo(25)
            make.bottom.equalTo(timerLab).offset(-30)
        }
        playPauseBtn.snp.makeConstraints { make in
            make.centerY.equalTo(self).offset(30)
            make.centerX.equalTo(self).offset(-90)
            make.width.equalTo(80)
            make.height.equalTo(35)
        }
        lapRestBtn.snp.makeConstraints { make in
            make.centerY.equalTo(self).offset(30)
            make.centerX.equalTo(self).offset(90)
            make.width.equalTo(80)
            make.height.equalTo(35)
        }
    }
    // MAKR: Action
    @objc func playBtnAction(sender: UIButton) {
        if delegate != nil {
            delegate!.stopWatchPlayAction()
        }
    }
    @objc func lapBtnAction(sender: UIButton) {
        if delegate != nil {
            delegate!.stopWatchLapAction()
        }
    }
}
